﻿/*
 *  Copyright 2012 Cloudbase Solutions Srl
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the Microsoft Public License (Ms-PL).  
 */

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Management.Server;
using Cloudbase.Iis.Utility;
using System.Net;
using Cloudbase.Iis.HostsFileManager.Common;
using Cloudbase.Iis.HostsFileManager.Model;
using System.Collections;

namespace Cloudbase.Iis.HostsFileManager.Server
{
    class HostsFileModuleService : ModuleService
    {
        [ModuleServiceMethod]
        public bool ContainsHost(string host)
        {
            return HostsFileManagerUtility.ContainsHost(host);
        }

        [ModuleServiceMethod]
        public void AddHost(string host, string ipAddress, string comment)
        {
            HostsFileManagerUtility.AddHost(host, IPAddress.Parse(ipAddress), comment);
        }

        [ModuleServiceMethod]
        public void UpdateHost(string originalHost, string host, string ipAddress, string comment)
        {
            HostsFileManagerUtility.UpdateHost(originalHost, host, IPAddress.Parse(ipAddress), comment);
        }

        [ModuleServiceMethod]
        public IList GetAllEntries()
        {
            IList l = new ArrayList();
            foreach (var h in HostsFileManagerUtility.GetAllEntries())
            {
                PropertyBag pb = new PropertyBag();
                pb.Add(0, h.Host);
                pb.Add(1, h.IPAddress.ToString());
                pb.Add(2, h.Comment);
                l.Add(pb);
            }

            return l;
        }

        [ModuleServiceMethod]
        public string GetHostsFilePath()
        {
            return HostsFileManagerUtility.GetHostsFilePath();
        }

        [ModuleServiceMethod]
        public void RemoveHost(string host)
        {
            HostsFileManagerUtility.RemoveHost(host);
        }
    }
}
